<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="分类ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryId">
              <j-dict-select-tag
                type="list"
                v-model="model.categoryId"
                dictCode="ht_patent_category,name,id"
                placeholder="请选择分类ID"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="店铺ID"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="shopId"
              :disabled="isDisabledAuth('patent-supply:shop-permission')"
            >
              <j-search-select-tag v-model="model.shopId" dict="ht_shop,name,id" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sort">
              <a-input-number v-model="model.sort" placeholder="请输入排序" style="width: 100%" />
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="专利名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入专利名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="副标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="subname">
              <a-textarea v-model="model.subname" rows="4" placeholder="请输入副标题" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="SEO关键字" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="seoKeywords">
              <a-textarea v-model="model.seoKeywords" rows="4" placeholder="请输入SEO关键字" />
            </a-form-model-item>
          </a-col>

          <a-col :span="24">
            <a-form-model-item label="SEO描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="seoDescription">
              <a-textarea v-model="model.seoDescription" rows="4" placeholder="SEO描述" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="专利号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="patentNumber">
              <a-input v-model="model.patentNumber" placeholder="请输入专利号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="交易费" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="transactionFee">
              <a-input-number v-model="model.transactionFee" placeholder="不填或者为0时表示商议" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="授权方式(0-专利转让，1-挂牌交易)"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="authorizationMethod"
            >
              <j-dict-select-tag
                type="list"
                v-model="model.authorizationMethod"
                dictCode="patent_authorization_method"
                placeholder="请选择授权方式(0-专利转让，1-挂牌交易)"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description">
              <a-input v-model="model.description" placeholder="请输入描述"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="详细内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="content">
              <j-editor v-model="model.content" />
            </a-form-model-item>
          </a-col>
          <video-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" v-model="model.videoUrls" />
          <a-col :span="24">
            <a-form-model-item label="主图" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cover">
              <j-image-upload :isMultiple="false" v-model="model.cover"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="图片集" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="images">
              <j-image-upload isMultiple v-model="model.images"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col v-has="'patent-supply:home-recommended-permission'" :span="24">
            <a-form-model-item
              label="是否为首页推荐 0-不推荐 1-推荐"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="isHomeRecommended"
            >
              <j-dict-select-tag
                type="list"
                v-model="model.isHomeRecommended"
                dictCode="is_home_recommended"
                placeholder="请选择是否为首页推荐 0-不推荐 1-推荐"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="供应状态（状态 -1-下架,1-上架,2-预售,0-未上架）"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="supplyStatus"
            >
              <j-dict-select-tag
                type="list"
                v-model="model.supplyStatus"
                dictCode="patent_supply_status"
                placeholder="请选择供应状态（状态 -1-下架,1-上架,2-预售,0-未上架）"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item
              label="询价企业数"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              prop="inquiryEnterpriseCount"
            >
              <a-input-number
                v-model="model.inquiryEnterpriseCount"
                placeholder="请输入询价企业数"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="发布日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="releaseDate">
              <j-date placeholder="请选择发布日期" v-model="model.releaseDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="截止日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="expireDate">
              <j-date
                placeholder="不选择即为无截止日期，用户可一直咨询"
                v-model="model.expireDate"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>

          <div v-has="'patent-supply:audit-permission'">
            <a-col :span="24">
              <a-form-model-item label="审批人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="auditUsername">
                <a-input v-model="model.auditUsername" placeholder="请输入审批人"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="审核状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="auditStatus">
                <j-dict-select-tag
                  type="list"
                  v-model="model.auditStatus"
                  dictCode="audit_status"
                  placeholder="请选择审核状态"
                />
              </a-form-model-item>
            </a-col>
            <a-col :span="24">
              <a-form-model-item label="审批意见" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="auditComments">
                <a-textarea v-model="model.auditComments" rows="4" placeholder="请输入审批意见" />
              </a-form-model-item>
            </a-col>
          </div>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { mapActions, mapState } from 'vuex'
import { DisabledAuthFilterMixin } from '@/mixins/DisabledAuthFilterMixin'

export default {
  name: 'HTPatentSupplyForm',
  mixins: [DisabledAuthFilterMixin],
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        authorizationMethod: 0,
        isHomeRecommended: 0,
        supplyStatus: '1',
        auditStatus: 0
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        shopId: [{ required: true, message: '请输入店铺ID!' }],
        categoryId: [{ required: true, message: '请输入分类ID!' }],
        name: [{ required: true, message: '请输入专利名称!' }],
        patentNumber: [{ required: true, message: '请输入专利号!' }],
        transactionFee: [{ required: false }, { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!' }],
        authorizationMethod: [{ required: true, message: '请输入授权方式(0-专利转让，1-挂牌交易)!' }],
        content: [{ required: true, message: '请输入详细内容!' }],
        cover: [{ required: true, message: '请输入主图!' }],
        isHomeRecommended: [{ required: true, message: '请输入是否为首页推荐 0-不推荐 1-推荐!' }],
        supplyStatus: [{ required: true, message: '请输入供应状态（状态 -1-下架,1-上架,2-预售,0-未上架）!' }],
        releaseDate: [{ required: true, message: '请输入发布日期!' }],
        // expireDate: [{ required: true, message: '请输入截止日期（不选择即为不截止）!' }],
        auditStatus: [{ required: true, message: '请输入审核状态!' }]
      },
      url: {
        add: '/patent/hTPatentSupply/add',
        edit: '/patent/hTPatentSupply/edit',
        queryById: '/patent/hTPatentSupply/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
    ...mapState('rolePermission', {
      shopPermission: state => state.shopPermission
    })
  },

  watch: {
    'shopPermission.shopId': {
      handler: function(newValue, oldValue) {
        if (newValue) {
          this.model.shopId = newValue
          this.$forceUpdate()
        }
      },
      immediate: true
    }
  },

  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    ...mapActions('rolePermission', ['getShopPermissionAction']),
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.getShopPermissionAction()
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    }
  }
}
</script>

